﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

namespace _91cc.Main.Config
{
    public class RSSConfig
    {
        private bool _OnClick = true;//true 单击 false 双击

        public bool OnClick
        {
            get { return _OnClick; }
            set { _OnClick = value; }
        }

        private List<RssBase> _rssbase;
        public List<RssBase> RssBases
        {
            get { return _rssbase; }
            set { _rssbase = value; }
        }

        private static object _obj = new object();

        private static RSSConfig _Instance = null;

        public static RSSConfig Instance
        {
            get
            {
                if (_Instance == null)
                {
                    ReadConfig();
                }
                return _Instance;
            }
            private set
            {
                _Instance = value;
            }
        }

        public static void ReadConfig()
        {
            if (!File.Exists(GetConfigPath()))
            {
                RSSConfig.Instance = new RSSConfig();
                RssBase rssbase = new RssBase();
                rssbase.Caption = "博客园";
                rssbase.Link = "http://www.cnblogs.com/rss.aspx";
                List<RssBase> list = new List<RssBase>();
                list.Add(rssbase);
                RSSConfig.Instance.RssBases = list;
                SavaConfig(RSSConfig.Instance);
            }
            else
            {
                XmlSerializer m_XmlSerializer = new XmlSerializer(typeof(RSSConfig));
                Stream m_Stream = new FileStream(GetConfigPath(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                RSSConfig.Instance = (RSSConfig)m_XmlSerializer.Deserialize(m_Stream);
                m_Stream.Close();
            }
        }
        public static void SavaConfig(RSSConfig _rssconfig)
        {
            lock (_obj)
            {
                XmlSerializer m_XmlSerializer = new XmlSerializer(typeof(RSSConfig));
                Stream m_Stream = new FileStream(GetConfigPath(), FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                m_XmlSerializer.Serialize(m_Stream, _rssconfig);
                m_Stream.Close();
            }
        }
        public static string GetConfigPath()
        {
            return System.Windows.Forms.Application.StartupPath + "\\RssConfig.xml";
        }
        public static Config.RssBase Find(string caption)
        {
            RssBase result = null;
            foreach (RssBase item in RSSConfig.Instance.RssBases)
            {
                if (string.Compare(caption, item.Caption, true) == 0)
                {
                    result = item;
                    break;
                }
            }
            return result;
        }
        public static void DeleteItem(string caption)
        {
            if (string.IsNullOrEmpty(caption)) return;
            RssBase rssBase = Find(caption);
            if (rssBase == null) return;
            Config.RSSConfig.Instance.RssBases.Remove(rssBase);
        }
    }
    public class RssBase
    {
        private string _caption;
        private string _link;

        public string Caption
        {
            get { return _caption; }
            set { _caption = value; }
        }
        public string Link
        {
            get { return _link; }
            set { _link = value; }
        }
    }
}
